package com.jdd.modules.parking.controller;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.jdd.common.api.vo.Result;
import com.jdd.common.aspect.annotation.AutoLog;
import com.jdd.modules.parking.entity.ParkArmInfo;
import com.jdd.modules.parking.entity.ParkInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;


/**
 * @author jdd
 */
@Slf4j
@Api(tags="停车二维码详情")
@RestController
@RequestMapping("/jddhlw/parkingQR")
public class ParkingQrController {

    /**
     *   二维码生成
     *
     */
    @AutoLog(value = "车场二维码生成")
    @ApiOperation(value="二维码生成", notes="二维码生成")
    @GetMapping(value = "/parkInfoQRcode")
    public void parkInfoQRCode(ParkInfo parkInfo, HttpServletResponse response) {
        Result<ParkInfo> result = new Result<ParkInfo>();
        String contentUrl = "";
        try {
            String parkCode = parkInfo.getParkCode();
            String imgPath = "";
            contentUrl = String.format(contentUrl,parkCode);
            Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(4);
            // 指定纠错等级
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            // 指定编码格式
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            hints.put(EncodeHintType.MARGIN, 1);
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contentUrl, BarcodeFormat.QR_CODE,
                    500,500
                    /*Integer.parseInt(parkVehicleDetail.getHeight()) , Integer.parseInt(parkVehicleDetail.getWidth())*/, hints);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            BufferedImage image = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
                            : 0xFFFFFFFF);
                }
            }
            OutputStream out = response.getOutputStream();
            ImageIO.write(image,"png",out);
            out.flush();
            out.close();
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
    }

    /**
     *   二维码生成
     *
     */
    @AutoLog(value = "出入口二维码生成")
    @ApiOperation(value="二维码生成", notes="二维码生成")
    @GetMapping(value = "/parkArmInfoQRcode")
    public void parkArmQRCode(ParkArmInfo parkArmInfo, HttpServletResponse response) {
        Result<ParkInfo> result = new Result<ParkInfo>();
        String contentUrl = "";
        try {
            String parkCode = parkArmInfo.getParkCode();
            String serialNo = parkArmInfo.getSerialNo();
            String imgPath = "";
            contentUrl = String.format(contentUrl,parkCode,serialNo);
            Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(4);
            // 指定纠错等级
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            // 指定编码格式
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            hints.put(EncodeHintType.MARGIN, 1);
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contentUrl, BarcodeFormat.QR_CODE,
                    500,500
                    /*Integer.parseInt(parkVehicleDetail.getHeight()) , Integer.parseInt(parkVehicleDetail.getWidth())*/, hints);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            BufferedImage image = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
                            : 0xFFFFFFFF);
                }
            }
            OutputStream out = response.getOutputStream();
            ImageIO.write(image,"png",out);
            out.flush();
            out.close();
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
    }
}
